package me.mingshan.leetcode;

/**
 * 21. 合并两个有序链表
 * <p>
 * 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
 * <p>
 * https://leetcode-cn.com/problems/merge-two-sorted-lists/
 */
public class L_21_MergeTwoLists {

    public static void main(String[] args) {
        ListNode next3 = new ListNode(4);
        ListNode next2 = new ListNode(3);
        ListNode next1 = new ListNode(2);
        ListNode head = new ListNode(1);
        head.next = next1;
        head.next.next = next2;
        head.next.next.next = next3;

        ListNode next13 = new ListNode(6);
        ListNode next12 = new ListNode(5);
        ListNode next11 = new ListNode(3);
        ListNode head1 = new ListNode(1);
        head1.next = next11;
        head1.next.next = next12;
        head1.next.next.next = next13;
        ListNode listNode = mergeTwoLists1(head, head1);
        ListNode.print(listNode);
    }

    /**
     * 思路：
     *
     * 创建一个虚拟节点，作为新链表的头节点
     * 分别遍历两个链表，依次比较两个链表的值，谁的值小，谁的指针向前移动，将其添加到新链表中
     *
     * 循环结束后，两个原链表有可能还没有遍历完，这些剩余的数据都是大于前面的数据的
     * 将剩余的数据添加到新链表中
     *
     *
     * 最终返回虚拟头节点的next节点
     *
     *
     * @param l1
     * @param l2
     * @return
     */
    public static ListNode mergeTwoLists1(ListNode l1, ListNode l2) {
        ListNode dummyNode = new ListNode(0);

        ListNode newCurr = dummyNode;

        ListNode curr1 = l1;
        ListNode curr2 = l2;

        while (curr1 != null && curr2 != null) {
            // 谁小谁先走
            if (curr1.val < curr2.val) {
                ListNode next = curr1.next;
                newCurr.next = curr1;

                curr1.next = null;
                curr1 = next;
                newCurr = newCurr.next;
            } else {
                ListNode next = curr2.next;

                newCurr.next = curr2;
                curr2.next = null;
                curr2 = next;
                newCurr = newCurr.next;
            }
        }

        // 走到最后，可能剩余的链表还有数据，这些数据添加到新链表中
        if (curr1 != null) {
            newCurr.next = curr1;
        }

        if (curr2 != null) {
            newCurr.next = curr2;
        }

        return dummyNode.next;
    }


    /**
     * 依次比较两个链表的值，谁的值小，谁的指针向前，最后将某一个链表剩余的元素全部拷贝过去即可
     *
     * @param l1
     * @param l2
     * @return
     */
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }

        if (l2 == null) {
            return l1;
        }

        ListNode next = null;
        ListNode head = null;

        ListNode l1Next = l1;
        ListNode l2Next = l2;

        while (l1Next != null && l2Next != null) {
            if (l1Next.val > l2Next.val) {
                if (next == null) {
                    next = new ListNode(l2Next.val);
                } else {
                    next.next = new ListNode(l2Next.val);
                }

                // 谁的值小，谁继续前进
                l2Next = l2Next.next;
            } else {
                if (next == null) {
                    next = new ListNode(l1Next.val);
                } else {
                    next.next = new ListNode(l1Next.val);
                }

                // 谁的值小，谁继续前进
                l1Next = l1Next.next;
            }

            if (head == null) {
                head = next;
            }

            if (next.next != null) {
                next = next.next;
            }
        }

        while (l1Next != null) {
            next.next = new ListNode(l1Next.val);
            l1Next = l1Next.next;

            next = next.next;
        }

        while (l2Next != null) {
            next.next = new ListNode(l2Next.val);
            l2Next = l2Next.next;

            next = next.next;
        }

        return head;
    }

    /**
     * 递归版:
     * <p>
     * 思路：比较两个链表的当前节点，如果l1的节点小于l2的节点，那么我们需要合并l1.next 和l2,
     * 两个合并之后再作为一个链表，且l1.next指向该链表
     *
     * @param l1
     * @param l2
     * @return
     */
    public static ListNode mergeTwoLists2(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }

        if (l2 == null) {
            return l1;
        }

        // 谁小谁前进
        if (l1.val < l2.val) {
            l1.next = mergeTwoLists2(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists2(l1, l2.next);
            return l2;
        }
    }

}
